global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A10_placebo_variants.log, replace name(tabl)

capture noi {

    * Run robustnessess specifications of the table_placebo
    qui do ${code_dir}/config/tabletools.do
    use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear


    estimates clear
    tab year, gen(YY_)
    clonevar LSW = lswMPm_1995_a
    clonevar HSW = hswMPm_1995_a
    clonevar VAEMP = vaempMPm_1995_a
    clonevar GDPGAP = lngdpgap_1995_a
    clonevar GDPPC = gdppcMPm_1995_a

    clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a
    clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
    clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
    clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
    clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a

    *First: Pauto90 REFINED restr. to baseline firms
    preserve 
    mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(none)
    global depvar "pauto90_rm6"

    clonevar stockown = k${depvar}_${ttt} 
    clonevar stockownzero = k${depvar}_${ttt}0  
    clonevar stockother = kNOT_${depvar}_${ttt} 
    clonevar stockotherzero = kNOT_${depvar}_${ttt}0
    clonevar spilloversown = spill${depvar}${ttt}_1995_a
    clonevar spilloversother = spillN${depvar}${ttt}_1995_a
    clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
    clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0

    bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
    bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
    drop _total_${depvar}_${ttt}_1995

    sort lse_id year
    egen yearctry = group(year country_shr_1995) if year <= 2009
    egen yearindustry = group(year industry) if year <= 2009
    egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
    *1) Pauto90 refined | VAEMP | domestic + foreign | F + IY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_1
    *2) Pauto90 refined | VAEMP | domestic + foreign | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_2
    *3) Pauto90 refined | VAEMP | foreign | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_3
    restore

    * Second: pauto90 (all firms)
    preserve
    global depvar "pauto90"
    clonevar stockown = k${depvar}_${ttt} 
    clonevar stockownzero = k${depvar}_${ttt}0  
    clonevar stockother = kNOT_${depvar}_${ttt} 
    clonevar stockotherzero = kNOT_${depvar}_${ttt}0
    clonevar spilloversown = spill${depvar}${ttt}_1995_a
    clonevar spilloversother = spillN${depvar}${ttt}_1995_a
    clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
    clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
    bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
    bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
    drop _total_${depvar}_${ttt}_1995

    egen yearctry = group(year country_shr_1995) if year <= 2009
    egen yearindustry = group(year industry) if year <= 2009
    egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
    sort lse_id year
    *4) Pauto90 | VAEMP | domestic + foreign | F + IY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_4

    gen in_sample = (e(sample))
    bys lse_id: egen f_in_sample = max(in_sample)
    keep if f_in_sample
    keep if year >= 1997 & year <= 2011
    egen tot_pauto90_bia = sum(pauto90_bia)
    egen tot_pauto90_rm6 = sum(pauto90_rm6)
    su tot_pauto*
    
    *5) Pauto90 | VAEMP | domestic + foreign | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_5
    *6) Pauto90 | VAEMP | foreign | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & total_${depvar}_${ttt}_1995>0, vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_6
    restore

    * Third: pauto95 restricted to pauto95 baseline firms
    preserve 
    mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(none)
    global depvar "pauto95"

    clonevar stockown = k${depvar}_${ttt} 
    clonevar stockownzero = k${depvar}_${ttt}0  
    clonevar stockother = kNOT_${depvar}_${ttt} 
    clonevar stockotherzero = kNOT_${depvar}_${ttt}0
    clonevar spilloversown = spill${depvar}${ttt}_1995_a
    clonevar spilloversother = spillN${depvar}${ttt}_1995_a
    clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
    clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0

    egen yearctry = group(year country_shr_1995) if year <= 2009
    egen yearindustry = group(year industry) if year <= 2009
    egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
    sort lse_id year
    *7) Pauto95 | VAEMP | domestic + foreign | F + IY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 , vce(cluster lse_id)  absorb(lse_id yearindustry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\no"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_7
    *8) Pauto95 | VAEMP | domestic + foreign | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 , vce(cluster lse_id) absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_8
    *9) Pauto95 | VAEMP | foreign | F + IY + CY
    ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & maxweight_1995 < 1 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 , vce(cluster lse_id)  absorb(lse_id yearindustry yearctry)
    estadd local f "\yes"
    estadd local iy "\yes"
    estadd local cy "\yes"
    hasvar stock* spill*, local(stockspill)
    estadd local obs "{\num{`e(N)'}}"
    estadd local firms "{\num{`e(N_clust)'}}"
    estimates store col_9
    restore

    qui do ${code_dir}/config/tabletools.do
    * Output table
    setlabels, sname("own")
    dhoztab col_1 col_2 col_3 col_4 col_5 col_6 col_7 col_8 col_9 using ${tab_dir}/appendix/Table_A10_placebo_variants.tex, b(%10.2f) se(%10.2f) nogaps replace numbers nonotes nolines showdepvarinfo nomtitles sfmt(a1) scalars("has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Number of firms") label mgroups("Dom. and Fgn." "Fgn." "Dom. and Fgn." "Fgn." "Dom. and Fgn." "Fgn.", pattern(1 0 1 1 0 1 1 0 1) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(stock* spill* _cons) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPgap_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC F2.placebo Placebo F2.placebozero Placebozero F2.logpauto90 Pauto90 F2.anypauto90 Anypauto90) ///
        depvar(none) ///
        rawaddheader({Dependent variable} &\multicolumn{3}{c}{Pauto90 refined}&\multicolumn{3}{c}{Pauto90}&\multicolumn{3}{c}{Pauto95} \\ \cmidrule(lr){2-4}\cmidrule(lr){5-7}\cmidrule(lr){8-10}) ///
        notes("This table presents additional regressions using non-automation innovations. In columns 1--3 the dependent variable is refined pauto90 (non-auto90 machinery patents that list at least one 4-digit C/IPC code containing a 6-digit code classified auto95), and the sample is restricted to the firms in the baseline auto95 regressions. In columns 4--6 the dependent variable is pauto90 (machinery patents excluding auto90) but the sample is unrestricted. In columns 7--9 the dependent variable is pauto95 (machinery patents excluding auto95), and the sample is again restricted to the firms in the baseline auto95 regression. All columns include firm and industry-year fixed effects, Columns 2, 3, 5, 6, 8 and 9 add country-year fixed effects. In Columns 3, 6, and 9 the macroeconomic variables are the normalized foreign variables as defined in the text. Stocks and spillovers are defined in terms of the respective dependent variable. Standard errors are clustered at the firm-level and reported in parentheses.")
    *again, } brackets
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl